﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DigDes.DSchool.SUPS.DataAccess.FileLoaders
{
    public class Dislocation70Loader : ILoader<Dislocation70LoaderDTO>
    {
        readonly int correctLineLength = 70;
        public bool Check(string filePath)
        {

            if (!filePath.ToLower().EndsWith(".out") || !File.Exists(filePath))
            {
                return false;
            }

            using (StreamReader streamReader = File.OpenText(filePath))
            {
                bool isLineCorrect = (streamReader.ReadLine().Length == correctLineLength);
                streamReader.Close();
                return isLineCorrect;
            }
        }

        public IEnumerable<Dislocation70LoaderDTO> Load(string filePath)
        {
            using (StreamReader streamReader = File.OpenText(filePath))
            {
                while (!streamReader.EndOfStream)
                {
                    string currentString = streamReader.ReadLine();
                    var row = new Dislocation70LoaderDTO();
                    int carNumber;
                    if (Int32.TryParse(currentString.Substring(0, 8), out carNumber))
                    {
                        row.CarNumber = carNumber;
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int weight;
                    if (Int32.TryParse(currentString.Substring(8, 3), out weight))
                    {
                        row.Weight = weight;
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int arrivalStation;
                    if (Int32.TryParse(currentString.Substring(11, 5), out arrivalStation))
                    {
                        row.ArrivalStation = arrivalStation;
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    //int cargoCode;
                    //if (Int32.TryParse(currentString.Substring(16, 5), out cargoCode))
                    //{
                        row.CargoCode = currentString.Substring(16, 5);
                    //}
                    //else
                    //{
                    //    row = null;
                    //    break;
                    //}

                    int consigneeCode;
                    if (Int32.TryParse(currentString.Substring(21, 4), out consigneeCode))
                    {
                        row.ConsigneeCode = currentString.Substring(21, 4);
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int carParkType;
                    if (Int32.TryParse(currentString.Substring(25, 2), out carParkType))
                    {
                        row.CarParkType = currentString.Substring(25, 2);
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int dispatchStation;
                    if (Int32.TryParse(currentString.Substring(27, 5), out dispatchStation))
                    {
                        row.DispatchStation = dispatchStation;
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int operationCode;
                    if (Int32.TryParse(currentString.Substring(32, 2), out operationCode))
                    {
                        row.OperationCode = currentString.Substring(32, 2);
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int operationDay;
                    int operationMonth;
                    int operationYear;
                    int operationHour;
                    int operationMinute;
                    if (Int32.TryParse(currentString.Substring(34, 2), out operationDay) &&
                        Int32.TryParse(currentString.Substring(36, 2), out operationMonth) &&
                        Int32.TryParse(currentString.Substring(38, 2), out operationYear) &&
                        Int32.TryParse(currentString.Substring(40, 2), out operationHour) &&
                        Int32.TryParse(currentString.Substring(42, 2), out operationMinute))
                    
                        row.OperationDateTime = new DateTime(operationYear + 2000, operationMonth, operationDay, operationHour, operationMinute, 0);
                    

                    int operationStation; 
                    if (Int32.TryParse(currentString.Substring(44, 5), out operationStation))
                    {
                        row.OperationStation = operationStation; 
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int roadOfDelivery;
                    if (Int32.TryParse(currentString.Substring(49, 2), out roadOfDelivery))
                    {
                        row.RoadOfDelivery = roadOfDelivery;
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int roadOfReception;
                    if (Int32.TryParse(currentString.Substring(51, 2), out roadOfReception))
                    {
                        row.RoadOfReception = roadOfReception;
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                  

                    long trainIndex;
                    if (long.TryParse(currentString.Substring(53, 13), out trainIndex))
                    {
                        row.TrainIndex = currentString.Substring(53, 13);
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    int trainNumber;
                    if (Int32.TryParse(currentString.Substring(66, 4), out trainNumber))
                    {
                        row.TrainNumber = currentString.Substring(66, 4);
                    }
                    else
                    {
                        row = null;
                        break;
                    }

                    yield return row;
                    
                }
                streamReader.Close();
            }
        }
    }
}
